package com.flyco.modules.stm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.flyco.modules.pms.service.IPmsSkuStockService;
import com.flyco.modules.stm.entity.StmChannelInfo;
import com.flyco.modules.stm.entity.StmChannelProduct;
import com.flyco.modules.stm.entity.StmChannelStoreRelation;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.stm.mapper.StmChannelStoreRelationMapper;
import com.flyco.modules.stm.service.IStmChannelInfoService;
import com.flyco.modules.stm.service.IStmChannelProductService;
import com.flyco.modules.stm.service.IStmChannelStoreRelationService;
import com.flyco.modules.stm.service.IStmStoreInfoService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 渠道供应商关系表
 * @Author: flyco
 * @Date: 2023-06-21
 * @Version: V1.0
 */
@Service
public class StmChannelStoreRelationServiceImpl extends ServiceImpl<StmChannelStoreRelationMapper, StmChannelStoreRelation> implements IStmChannelStoreRelationService {

    @Autowired
    private StmChannelStoreRelationMapper relationMapper;
    @Autowired
    private IStmChannelInfoService channelInfoService;
    @Autowired
    private IStmStoreInfoService stmStoreInfoService;
    @Autowired
    private IPmsSkuStockService skuStockService;
    @Autowired
    private IStmChannelProductService channelProductService;

    @Override
    public void deleteByStoreId(Long storeId) {
        relationMapper.deleteByStoreId(storeId);

    }

    @Override
    public void saveByChannelIds(String storeIdStr,Long storeId, String channelIds) {

        //查询供应商现有的渠道，如果一样，则不处理，否则进行增加或删除。
        List<StmChannelStoreRelation> relationList = getByStoreId(storeId);
        List<Integer> storeChannelIds = relationList.stream().map(o -> o.getChannelId()).collect(Collectors.toList());
        //获取新增的渠道
        List<Integer> needSaveChannelIds = getStoreNotSaveChannelIds(storeChannelIds, channelIds);

        //需要删除的渠道
        List<Integer> needDeleteChannelIds = getStoreNeedDeleteChannelIds(storeChannelIds, channelIds);

        StmChannelStoreRelation channelStoreRelation;
        List<StmChannelStoreRelation> channelStoreRelationList = new ArrayList<>();
        for (Integer channelId : needSaveChannelIds) {
            //保存经销商的渠道
            channelStoreRelation = new StmChannelStoreRelation();
            channelStoreRelation.setStoreId(storeId);
            channelStoreRelation.setChannelId(channelId);
            channelStoreRelation.setChannelIdStr(channelId.toString());
            channelStoreRelation.setStoreIdStr(storeIdStr);
            StmChannelInfo channelInfo = channelInfoService.getById(channelId);
            if(channelInfo!=null){
                channelStoreRelation.setChannelName(channelInfo.getName());
            }

            channelStoreRelationList.add(channelStoreRelation);
            save(channelStoreRelation);

            //找出渠道下面的商品,进行保存
            saveByChannelId(channelId,storeId);
        }

        for (Integer channelId : needDeleteChannelIds) {
            deleteByStoreIdAndChannelId(storeId, channelId);
            //不需要删除sku表的信息
        }

        //更新门店的channelNames字段。
        updateStoreChannelName(channelIds, storeId);
    }

    /**
     * 针对一个渠道，把绑定该渠道的经销商对于的sku表都进行保存，有的忽略
     */
    @Override
    public void saveByChannelId(Integer channelId,Long storeId){

        List<StmChannelProduct> channelProductList = channelProductService.getByChannelId(channelId);
        for (StmChannelProduct product : channelProductList) {
            //往sku表添加信息
            skuStockService.addByProductSnAndStoreId(product.getProductSn(), storeId, channelId);
        }
    }

    private void updateStoreChannelName(String channelIds, Long storeId) {
        String channelNames="";
        if(!StringUtils.isEmpty(channelIds)){
            List<String> nameList = new ArrayList<>();
            List<String> channelIdStr = Arrays.asList(channelIds.split(","));
            channelIdStr.forEach(o -> {
                StmChannelInfo channelInfo = channelInfoService.getById(Integer.valueOf(o));
                nameList.add(channelInfo.getName());
            });
            channelNames = nameList.stream().collect(Collectors.joining(","));
        }

        //更新门店的channelNames字段。
        StmStoreInfo storeInfo = stmStoreInfoService.getByStoreId(storeId);
        storeInfo.setChannelName(channelNames);
        stmStoreInfoService.updateById(storeInfo);
    }


    @Override
    public Integer updateStatusByChannelId(Integer status, Integer channelId) {
        return relationMapper.updateStatusByChannelId(status, channelId);
    }

    @Override
    public List<StmChannelStoreRelation> getByStoreId(Long storeId) {

        LambdaQueryWrapper<StmChannelStoreRelation> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(StmChannelStoreRelation::getStoreId, storeId);
        return list(queryWrapper);
    }

    //找出经销商没有的渠道id
    private List<Integer> getStoreNotSaveChannelIds(List<Integer> storeChannelIds, String channelIds) {
        List<Integer> needSaveChannelIds = new ArrayList<>();
        List<String> channelIdStr = Arrays.asList(channelIds.split(","));
        channelIdStr.forEach(o -> {
            if (!StringUtils.isEmpty(o) && !storeChannelIds.contains(Integer.valueOf(o))) {
                needSaveChannelIds.add(Integer.valueOf(o));
            }
        });
        return needSaveChannelIds;
    }

    //找出经销商需要删除的渠道Id
    private List<Integer> getStoreNeedDeleteChannelIds(List<Integer> storeChannelIds, String channelIds) {
        List<Integer> needDeleteChannelIds = new ArrayList<>();
        List<String> channelIdStr = Arrays.asList(channelIds.split(","));
        List<Integer> ChannelIdList = new ArrayList<>();
        channelIdStr.forEach(o -> {
            if (!StringUtils.isEmpty(o)) {
                ChannelIdList.add(Integer.valueOf(o));
            }
        });
        storeChannelIds.forEach(channelId -> {
            if (!ChannelIdList.contains(channelId)) {
                needDeleteChannelIds.add(channelId);
            }
        });

        return needDeleteChannelIds;
    }


    @Override
    public void deleteByStoreIdAndChannelId(Long storeId, Integer channelId) {
        relationMapper.deleteByStoreIdAndChannelId(storeId, channelId);
    }

    @Override
    public List<StmChannelStoreRelation> getByChannelId(Integer channelId){
        LambdaQueryWrapper<StmChannelStoreRelation> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(StmChannelStoreRelation::getChannelId,channelId);

        return list(wrapper);
    }

}
